﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using AjaxControlToolkit;

public partial class mwc_admin_aboutus : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        initPage();
        loadAboutList();
    }
    protected void initPage() {
        string task = Request.QueryString["task"];
        switch (task) { 
            case "add":
                pn_list_about.Visible = false;
                pn_detail_about.Visible = true;
                break;
            case "edit":
                loadAbout();
                pn_list_about.Visible = false;
                pn_detail_about.Visible = true;
                break;
            default:
                pn_list_about.Visible = true;
                pn_detail_about.Visible = false;
                break;
        }
    }
    protected void saveAbout(object sender, EventArgs e) { 
        string task = Request.QueryString["task"];
        switch (task) { 
            case "add":
                addAbout();
                break;
            case "edit":
                editAbout();
                break;
        }
    }
    protected void addAbout() {
        string enable = about_enable.Checked == true ? "1" : "0";
        string image = ConfigurationSettings.AppSettings["image_root_path"] + "aboutus/" + AsyncFileUpload1.FileName;
        string query = "INSERT INTO mwc_about_us(name,position,introtext,enable,image) VALUES('" + about_name.Text + "','" + about_pos.Text + "','" + about_introtext.Text + "'," + enable + ",'" + image + "')";
        SqlConnection sc = new SqlConnection(ConfigurationSettings.AppSettings["mwc_con"]);
        sc.Open();
        SqlCommand scmd = new SqlCommand(query, sc);
        scmd.ExecuteNonQuery();
        sc.Close();
        aboutRedirect();
    }
    protected void loadAbout() {
        string id = Request.QueryString["id"];
        string query = "SELECT * FROM mwc_about_us WHERE id = " + id;
        SqlConnection sc = new SqlConnection(ConfigurationSettings.AppSettings["mwc_con"]);
        sc.Open();
        SqlCommand scmd = new SqlCommand(query, sc);
        SqlDataReader sdr = scmd.ExecuteReader();
        while (sdr.Read()) {
            about_name.Text = sdr["name"].ToString();
            about_pos.Text = sdr["position"].ToString();
            about_introtext.Text = sdr["introtext"].ToString();
            bool _checked = sdr["enable"].ToString() == "True" ? true : false;
            about_enable.Checked = _checked;
        }
        sdr.Close();
        sc.Close();
    }
    protected void editAbout() {
        string id = Request.QueryString["id"];
        string _enable = about_enable.Checked == true ? "1" : "0";
        string query = "";
        if (AsyncFileUpload1.HasFile){
            query = "UPDATE mwc_about_us SET name = '" + about_name.Text + "', position = '" + about_pos.Text + "', introtext = '" + about_introtext.Text + "', enable = " + _enable + ", image = '" + ConfigurationSettings.AppSettings["image_root_path"] + "aboutus/" + AsyncFileUpload1.FileName + "' WHERE id = " + id;
        }
        else {
            query = "UPDATE mwc_about_us SET name = '" + about_name.Text + "', position = '" + about_pos.Text + "', introtext = '" + about_introtext.Text + "', enable = " + _enable + " WHERE id = " + id;
        }
        SqlConnection sc = new SqlConnection(ConfigurationSettings.AppSettings["mwc_con"]);
        sc.Open();
        SqlCommand scmd = new SqlCommand(query, sc);
        scmd.ExecuteNonQuery();
        sc.Close(); 
        aboutRedirect();

    }
    protected void aboutCancel(object sender, EventArgs e)
    {
        aboutRedirect();
    }
    protected void aboutRedirect() {
        Response.Redirect("~/mwc_admin/Panel.aspx?option=aboutus");
    }
    protected void uploadComplete(object sender, AsyncFileUploadEventArgs e)
    {
        if (AsyncFileUpload1.HasFile)
        {
            AsyncFileUpload1.SaveAs(ConfigurationSettings.AppSettings["upload_path"] + "aboutus/" + AsyncFileUpload1.FileName);
            //loadFile("");
        }
    }
    protected void loadAboutList() {
        string query = "SELECT * FROM mwc_about_us";
        SqlConnection sc = new SqlConnection(ConfigurationSettings.AppSettings["mwc_con"]);
        SqlDataAdapter sda = new SqlDataAdapter(query, sc);
        DataSet ds = new DataSet("list");
        sda.Fill(ds);
        about_list.DataSource = ds;
        about_list.DataBind();
    }
}
